package edu.map.aamas.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.map.aamas.gui.psp.PropertiesPanel;
import edu.map.aamas.rnn.RNN;

public class ControlPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private RNN rnr;
	
	public ControlPanel(RNN rnr) {
		this.rnr = rnr;
		setLayout(new FlowLayout(FlowLayout.LEFT));		
		
		AbstractAction action = new AbstractAction("Pause", new ImageIcon(getClass().getClassLoader().getResource("icons/stop.png"))) {
			private boolean paused = false;
			public void actionPerformed(ActionEvent e) {
				if (!paused) {
					paused = true;
					getRnr().pause();
					this.putValue(AbstractAction.NAME, "Resume");
				}
				else {
					paused = false;
					getRnr().resume();
					this.putValue(AbstractAction.NAME, "Pause");
				}
			}
		};				
		add(createButton(action));
		
		
		action = new AbstractAction("Settings", new ImageIcon(getClass().getClassLoader().getResource("icons/settings.png"))) {

			public void actionPerformed(ActionEvent e) {
				getRnr().pause();
				PropertiesPanel.editRNRProperties(getRnr());
				getRnr().resume();
			}
		};				
		add(createButton(action));
		
		
		action = new AbstractAction("Restart", new ImageIcon(getClass().getClassLoader().getResource("icons/restart.png"))) {

			public void actionPerformed(ActionEvent e) {
				getRnr().pause();
				getRnr().init();
				try {
					Thread.sleep(getRnr().millisBetweenIterations * 2);
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
				getRnr().resume();
			}
		};
		
		add(createButton(action));

	}
	
	public JButton createButton(AbstractAction action) {
		JButton btn = new JButton(action);
		btn.setMargin(new Insets(0,0,0,0));
		btn.setFocusPainted(false);
		btn.setSize(100, 26);
		btn.setPreferredSize(new Dimension(90,30));
		btn.setToolTipText((String)action.getValue(AbstractAction.NAME));
		btn.setAlignmentY(JButton.LEFT_ALIGNMENT);
		return btn;
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setSize(400, 32);
		frame.getContentPane().add(new ControlPanel(RNN.getInstance(32)));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public RNN getRnr() {
		return rnr;
	}

	public void setRnr(RNN rnr) {
		this.rnr = rnr;
	}
	

}
